import { createRouter, createWebHistory } from "vue-router";
import _, { findLastKey } from "lodash";
import { useUserStore } from "@/store/modules/user";
// console.log(modules)
export const childrenRouterMap = [
  {
    path: "/dashboard",
    component: () => import("@/views/dashboard/index.vue"),
    meta: {
      icon: "overview",
      title: "总览",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: true,
      isKeepAlive: true,
    },
  },
  {
    path: "/wallet",
    component: () => import("@/views/wallet/index.vue"),
    meta: {
      icon: "assets",
      title: "钱包",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: true,
      isKeepAlive: true,
    },
  },
  {
    path: "/validator",
    component: () => import("@/views/settings/components/Validator.vue"),
    meta: {
      icon: "HomeFilled",
      title: "匹配验证器应用",
      isLink: "",
      isHide: true,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      activeMenu: "/settings",
    },
  },
  {
    path: "/account/customer",
    component: () => import("@/views/account/management/index.vue"),
    meta: {
      icon: "customer",
      title: "用户",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      code: "web_customer",
    },
  },
  {
    path: "/account/gameRecord",
    component: () => import("@/views/account/gameRecord/index.vue"),
    meta: {
      icon: "customer",
      title: "游戏记录",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
      code: "web_customer",
    },
  },
  {
    path: "/settings",
    component: () => import("@/views/settings/index.vue"),
    meta: {
      icon: "identity",
      title: "设置",
      isLink: "",
      isHide: false,
      isFull: false,
      isAffix: false,
      isKeepAlive: true,
    },
  },
];

export const adminRoterMap = [];
export const asyncRouterMap = [];

export const staticRouter = [
  {
    path: "/",
    redirect: "/dashboard",
  },
  {
    path: "/layout",
    name: "layout",
    component: () => import("@/layout/index.vue"),
    redirect: "/dashboard",
    children: [...childrenRouterMap],
  },
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
    },
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/:pathMatch(.*)",
    name: "not-found",
    meta: {
      title: "404",
    },
    component: () => import("@/views/error/404.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: [...staticRouter],
  strict: false,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

/**
 * 判断用户是否有权限访问单个路由
 * route：访问的路由
 */
const hasPermission = (route) => {
  const useUser = useUserStore();
  const permissionList = useUser.currentPermissions.value;

  if (permissionList[0] === "all") {
    return true;
  }
  if (route.meta && route.meta.code) {
    // 判断route的code值是否存在于permissionList
    return permissionList.includes(route.meta.code);
  }
  return true;
};

/**
 * 筛选可访问的动态路由
 * routerMap：访问的动态列表
 */
const filterAsyncRouter = (routerMap) => {
  const accessedRouters = routerMap.filter((route) => {
    if (hasPermission(route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children);
      }
      return true;
    }
    return false;
  });
  return accessedRouters;
};

export const generateRouter = () => {
  const useUser = useUserStore();
  const permissionList = useUser.currentPermissions.value;
  if (["all"].includes(permissionList[0])) {
    adminRoterMap.forEach((item) => {
      router.addRoute("layout", item);
    });
  }

  const RouterMap = _.cloneDeep(asyncRouterMap);

  return new Promise((resolve) => {
    const validRouter = filterAsyncRouter(RouterMap);
    validRouter.forEach((item) => {
      router.addRoute("layout", item);
    });
    resolve(true);
  });
};

// asyncRouterMap.forEach((item) => {
//     router.addRoute("layout", item);
// })

export default router;
